/**
 * Copyright (c) 2011 - 2012, Sohu Inc. All rights reserved.
 * @fileoverview Sohu   表单-textarea组件类
 * @author  bowen liu | bowenliu@sohu-inc.com
 * @version 1.0 | 2012-06-29
 * @param
 * opt{
 * 	'el': '',       // textarea标签
 * 	'require':['focus','blur']    // 需要绑定的事件
 * }
 * form_data="isMust=1&isChange=1&value=1&max=20&min=1"
 * @example
 * var a = formTextarea(opt);
 */
$require('form.cFormTips');
$require('form.formTextarea')
LJS.define('form.cFormTextarea',function(ljs){
/*
 *$Import('module.form.formTextarea');
$Import('cpt.tipMsg');
$Import('tpl.formTextareaTpl');
 */
    var cFormTips = ljs.form.cFormTips;
    var _formTextarea = ljs.form.formTextarea;
    var formTextareaTpl = {
        tip: '只支持纯文本',
        error: '只支持纯文本，最多不超过140个汉字'
    }
    var formTextarea = function(opt){
        var that = this;
        if (!opt || !opt.el) 
            return false;
        // textarea标签
        that.el = opt.el;
        // 解form_data序列获得全局属性对象
        var _formData = that.el.getAttribute('form_data');
        that.data = typeof _formData === 'string' ? $.queryToJson(_formData) : {};
        // 是否是必选项
        that.isMust = parseInt(that.data.isMust) || 0;
        // 是否实时判断合法性
        that.changeStore = parseInt(that.data.isChange) || 0;
        that.isChange = parseInt(that.data.isChange) || 0;
        // 缺省值
        that.value = that.data.value || null;
        // 最大输入字数
        that.max = typeof that.data.max === 'string' ? parseFloat(that.data.max) : '';
        // 最小输入字数
        that.min = typeof that.data.min === 'string' ? parseFloat(that.data.min) : '';
        // 默认需要绑定的事件
        that.require = opt.require || [];
        // 此控件是否处于报错状态
        that.state = false;
        that.tipEl = opt.tipEl || null;
        that.arrow = opt.arrow;
        that.posObj = opt.posObj;
        that.formTextareaTpl = opt.formTextareaTpl || formTextareaTpl;
        that.init();
    };
    // 继承formTextarea
    formTextarea.prototype = new _formTextarea;
    formTextarea.prototype.constructor = _formTextarea
    formTextarea.prototype.init = function(){
        var that = this;
        that.value !== null ? that.setValue(that.value) : '';
        that.tip = new cFormTips({
            'el': that.el,
            'arrow' : that.arrow,
            'posObj' : that.posObj
        });
        var _changeTip = function(){
            if (!that.tipEl) 
                return false;
            var _num = that.max - that.getValueLength();
            //_num < 0 ? $.addClassName(that.tipEl, that.tpl.tipClassName) : $.removeClassName(that.tipEl, that.tpl.tipClassName);
            that.tipEl.innerHTML = parseInt(_num/2);
            if(parseInt(_num/2)<0){
              $(that.tipEl).css('color','#F00');  
            }else{
                $(that.tipEl).css('color','');
            }
            
        };
        
        $.foreach(that.require, function(a){
            switch (a) {
                case 'focus':
                    that.isChange = that.changeStore;
                    that.focus({
                        callback: function(){
                            _changeTip();
                            that.state === true ? '' : that.showTip();
                        },
                        loop: function(){
                            _changeTip();
                            that.check() ? that.hide() : that.showError();
                        }
                    });
                    break;
                case 'blur':
                    that.blur({
                        callback: function(){
                            if (that.check()) {
                                that.isChange = 0;
                                that.showRight();
                                that.state = false;
                            } else {
                                that.showError();
								//$.addClassName(that.el, 'error');
                                that.isChange = that.changeStore;
                                that.state = true;
                            }
                        }
                    });
                    break;
                case 'click':
                    that.click();
                    break;
                default:
                    return false;
            }
        });
        _changeTip();
    };
    /**
     * 检测
     * @return Boolean true || false
     * 当为非必填项时,直接返回true
     */
    formTextarea.prototype.check = function(){
        var that = this;
        // 不是必选
        if (!that.isMust) 
            return true;
		if(that.getValue()==that.el.getAttribute('definevalue')){
			return false
			}
        // 必选
        // 没给最大值、最小值
        if (typeof that.max === 'string' && typeof that.min === 'string') 
            return true;
        // 空值
        if (that.isEmpty()) 
            return false;
        // 只给了最小值
        if (typeof that.max === 'string') 
            return that.getValueLength() >= that.min;
        // 只给了最大值
        if (typeof that.min === 'string') 
            return that.getValueLength() <= that.max;
        return that.getValueLength() >= that.min && that.getValueLength() <= that.max;
    };
    // 显示提示层
    formTextarea.prototype.showTip = function(){
        var that = this;
		$.removeClassName(that.el,'error');
        that.tip.show(that.formTextareaTpl.tip);
    };
    // 显示错误提示层
    formTextarea.prototype.showError = function(){
        var that = this;
		$.addClassName(that.el, 'error');
        that.tip.show(that.formTextareaTpl.error,1);
    };
    formTextarea.prototype.showRight = function(){
     var that = this;
     that.tip.show('',2);
	 $.addClassName(that.el,'error');
    };
    // 隐藏层
    formTextarea.prototype.hide = function(){
        var that = this;
		$.removeClassName(that.el, 'error');
        that.tip.hide();
    };
    /**
     * 更新html标签的form_data序列
     * 同时更新全局属性对象that.data
     */
    formTextarea.prototype.J2Q = function(){
        var that = this;
        var opt = {
            'value': that.value,
            'isMust': parseInt(that.isMust) || 0,
            'isChange': parseInt(that.data.isChange) || 0,
            'max': that.max,
            'min': that.min
        };
        that.data = $.jsonToQuery(opt);
        that.el.setAttribute('form_data', that.data);
    };
    
    return formTextarea;
});
